package com.huakang.service.impl;

import com.huakang.entity.Account;
import com.huakang.entity.AccountExample;
import com.huakang.mapper.AccountMapper;
import com.huakang.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
@Transactional
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountMapper accountMapper;

    @Override
    public Account login(Account account) {
        AccountExample example = new AccountExample();
        AccountExample.Criteria criteria = example.createCriteria();
        criteria.andAnameEqualTo(account.getAname());
        criteria.andApasswordEqualTo(account.getApassword());
        List<Account> accounts = accountMapper.selectByExample(example);
        return accounts.size()==0?null:accounts.get(0);
    }

    @Override
    public boolean reg(Account account) {
        int i = accountMapper.insertSelective(account);
        return i>0?true:false;
    }

    @Override
    public List<Account> getAll() {
        return accountMapper.selectByExample(null);
    }

    @Override
    public boolean reset(Integer aid) {
        return accountMapper.updateByPrimaryKeySelective(new Account(aid,null,"888888",null,null))>0;
    }

    @Override
    public boolean updateStatus(Integer aid) {
       Account account = accountMapper.selectByPrimaryKey(aid);
       account.setAstatus(account.getAstatus().equals("可用")?"禁用":"可用");
       return accountMapper.updateByPrimaryKeySelective(account)>0;
    }
}
